package utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jdom.*;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import ClassPack.ClassDiagram;
import ClassPack.Class;

public class ClassDiagramFileWriter {

	
	 ClassDiagram 		m_ClassDiagram; 
	 Element	 		root;
	 String 			m_fileName;
	 
	 public ClassDiagramFileWriter(ClassDiagram cd , String FileName ){
		 
		 m_ClassDiagram = cd ;
		 m_fileName = FileName;
	 }
	 
	  public void CreateXMLroot(){
		  
		   root = new 
		  		Element( m_ClassDiagram.getName());
		  
		  for (Class c  : m_ClassDiagram.getM_classes() ) {
			
			  Element a_class = 		new Element( c.getM_class_name());
			  Element referenced_classes = new Element("Refenenced_classes");
			  
			  a_class.addContent(referenced_classes);
			  
			  for (Class ref_c  : c.getReferenced_classes() ) {
				 
				  Element ref_class = new Element(ref_c.getM_class_name());
				  referenced_classes.addContent(ref_class);
			  }
			  Element inhereted_interfaces = new Element("inhereted_interfaces");
			  a_class.addContent(inhereted_interfaces);
			  
			  for ( Class ref_i : c.getM_inhereted_interfaces()){
				  Element ref_class = new Element(ref_i.getM_class_name());
				  referenced_classes.addContent(ref_class);
			  }
			  
			  Element methods = new Element("Methods") ;
			  
			  for (String method : c.getM_methods()) {
				  methods.addContent(new Element(method));
			  }
			  
			  root.addContent(a_class);
		  
		  }
	  }
	  
	  
	  
	  public void WriteXMLtoFIle(){
		  
		  
	      
		  Document doc = new Document(root);
		    // serialize it onto System.out
		    try {
		    	XMLOutputter serializer = new XMLOutputter();
		    	serializer.setFormat(Format.getPrettyFormat());
		    	serializer.output(doc, new FileOutputStream(new File(m_fileName  )));
			      
		    }
		    catch (IOException e) {
		      System.err.println(e);
		    }
	  }
	  
	  
	  
	  public static void main(String[] args) {
		  
		  Class a = new Class();
		  Class b = new Class();
		  Class c = new Class();
		  
		  a.setM_class_name("a");
		  b.setM_class_name("b");
		  c.setM_class_name("c");
		  
		  a.addReferencedClass(b);
		  a.addReferencedClass(c);
		  
		  b.addReferencedClass(a);
		  b.addReferencedClass(c);
	  
		  c.addReferencedClass(a);
		  c.addReferencedClass(b);
	  
	  
		  ClassDiagram cd = new ClassDiagram();
		  cd.setName("cd");
		  cd.addClass(a);
		  cd.addClass(b);
		  cd.addClass(c);
		  
		  ClassDiagramFileWriter cdwr= new ClassDiagramFileWriter(cd,"sami.");
		  cdwr.CreateXMLroot();
		  cdwr.WriteXMLtoFIle();
		  
	  }
}
